import format from '@dfeidao/atom-web/dt/format';
import get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import p001 from './p001';
import p002 from './p002';
import p003 from './p003';
import p004 from './p004';

export default async function a002(fd: IFeidaoAiBrowserComponent) {
	// todo
	const exam_name1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="exam_name"]').value;
	if (exam_name1 === "") {
		const users = get('users');
		const res1 = await nodejs<{ dt: Array<{ exam_id: string, length: number }> }>('employee-examination/zj-000001/s001', {});
		const res2 = await nodejs<{ dt: Array<{ exam_id: string, paper_number: string }> }>('employee-examination/zj-000001/s002',
			{
				filter: {
					candidate_no: users.staff_no
				}
			});
		const ret = new Array();
		for (let i = 0; i < res2.dt.length; i++) {
			ret[i] = res2.dt[i].exam_id;
		}
		// 数组去重
		const ses = new Set(ret);
		const newArr = Array.from(ses);
		const a = Array();
		for (let i = 0; i < res1.dt.length; i++) {
			a.push(res1.dt[i].exam_id);
		}
		const res3 = await nodejs<{
			dt: Array<{
				exam_id: string,
				exam_name: string,
				exam_time: string,
				examination_category: string,
				exam_score: string,
				exam_time1: string,
				exam_caozuo: string
			}>; c: number
		}>('employee-examination/zj-000001/s003',
			{
				filter: {
					pn: 1,
					exam_id: a
				}
			});
		const res4 = await nodejs<{ dt: Array<{ exam_id: string, exam_score: string, exam_time: number }> }>('employee-examination/zj-000001/s004',
			{
				filter: {
					exam_id: a
				}
			});
		// 转时间格式
		const p = res4.dt.map((n) => {
			return { ...n, exam_time: format(n.exam_time, 2) };
		});
		const dat = res3.dt.map((n) => {
			for (let index = 0; index < res4.dt.length; index++) {
				if (res4.dt[index].exam_id === n.exam_id) {
					n.exam_time1 = p[index].exam_time;
					n.exam_score = res4.dt[index].exam_score;
					return n;
				}
			}
			n.exam_time1 = '';
			n.exam_score = '';
			return n;
		});
		// 操作
		const da = dat.map((n) => {
			for (let index = 0; index < newArr.length; index++) {
				if (newArr[index] === n.exam_id) {
					n.exam_caozuo = "1";
					return n;
				}
			}
			n.exam_caozuo = "0";
			return n;
		});
		// 获取那三个dot模板
		const dtData = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p001"]');
		const dtNoData = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p002"]');
		const dtPn = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p003"]');
		const dtPn1 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p004"]');
		dtPn1.style.display = 'none';
		if (da.length > 0) {
			render(fd, da, p001, 'p001', 'inner');
			if (res3.c > 20) {
				render(fd, res3.c, p003, 'p003', 'inner');
			}
			dtNoData.style.display = 'none';
			dtData.style.display = 'block';
			dtPn.style.display = 'block';
		} else {
			render(fd, '', p002, 'p002', 'inner');
			dtNoData.style.display = 'block';
			dtData.style.display = 'none';
			dtPn.style.display = 'none';
		}
	} else {
		const res = await nodejs<{ dt: Array<{ exam_id: string, exam_name: string, exam_time: string, examination_category: string, exam_score: string, exam_time1: string, exam_caozuo: string }>; c: Array<{}> }>('employee-examination/zj-000001/s005',
			{
				filter: {
					exam_name: exam_name1,
					pn: 1
				}
			});
		const a = Array();
		for (let i = 0; i < res.dt.length; i++) {
			a.push(res.dt[i].exam_id);
		}
		const res1 = await nodejs<{ dt: Array<{ exam_id: string, exam_score: string, exam_time: number }> }>('employee-examination/zj-000001/s006',
			{
				filter: {
					exam_id: a

				}
			});
		const p = res1.dt.map((n) => {
			return { ...n, exam_time: format(n.exam_time, 2) };
		});
		const dat = res.dt.map((n) => {
			for (let index = 0; index < res1.dt.length; index++) {
				if (res1.dt[index].exam_id === n.exam_id) {
					n.exam_time1 = p[index].exam_time;
					n.exam_score = res1.dt[index].exam_score;
					return n;
				}
			}
			n.exam_time1 = '';
			n.exam_score = '';
			return n;
		});
		const no = get("no");
		const res2 = await nodejs<{ dt: Array<{ exam_id: string }> }>('employee-examination/zj-000001/s002',
			{
				filter: {
					candidate_no: no
				}
			});
		const ret = new Array();
		for (let i = 0; i < res2.dt.length; i++) {
			ret[i] = res2.dt[i].exam_id;

		}
		// 数组去重
		const ses = new Set(ret);
		const newArr = Array.from(ses);
		// 操作
		const da = dat.map((n) => {
			for (let index = 0; index < newArr.length; index++) {
				if (newArr[index] === n.exam_id) {
					n.exam_caozuo = "1";
					return n;
				}
			}
			n.exam_caozuo = "0";
			return n;
		});
		// 获取那三个dot模板
		const dtData = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p001"]');
		const dtNoData = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p002"]');
		const dtPn = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p003"]');
		const dtPn1 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p004"]');
		dtPn.style.display = 'none';
		if (da.length > 0) {
			render(fd, da, p001, 'p001', 'inner');
			if (res.c.length > 20) {
				render(fd, res.c.length, p004, 'p004', 'inner');
				dtPn1.style.display = 'block';
			}
			dtNoData.style.display = 'none';
			dtData.style.display = 'block';
		} else {
			render(fd, '', p002, 'p002', 'inner');
			dtNoData.style.display = 'block';
			dtData.style.display = 'none';
			dtPn.style.display = 'none';

		}
	}

}
